<?php


namespace app\api\controller\v1;


use app\common\controller\Api;
use app\common\library\Sms;
use think\Db;
use think\Queue;

/**
 * 内容接口，均不需登入
 */
class Content extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 首页Banner
     * @ApiMethod (GET)
     */
    public function indexBanner()
    {
        if ($this->request->isGet()) {
            $result = Db::name('content_banner')->select();

            if ($result) {
                foreach ($result as &$item) {
                    $item['image'] = self::patchSingle($item['image']);
                }

                $this->success('获取成功', $result);
            } else {
                $this->error('获取失败');
            }
        } else {
            $this->error(ErrorCode::$ERROR['ERROR_METHOD']);
        }
    }

    /**
     * 用户协议
     * @ApiMethod (GET)
     */
    public function userContract()
    {
        if ($this->request->isGet()) {

            $result = Db::name('content_user_contract')->find();

            if ($result) {
                $result['content'] = self::patchContent($result['content']);

                $this->success('获取成功', $result['content']);
            } else {
                $this->error('获取失败');
            }
        } else {
            $this->error(ErrorCode::$ERROR['ERROR_METHOD']);
        }
    }

    /**
     * 公司合同
     * @ApiMethod (GET)
     */
    public function companyContract()
    {
        if ($this->request->isGet()) {

            $id = $this->request->param('id', false);
            $id == false && $this->error('id 参数不得为空');

            $result = Db::name('content_company_contract')->where(['company_id' => $id])->find();

            if ($result) {
                $result['content'] = self::patchContent($result['content']);
                $this->success('获取成功', $result['content']);
            } else {
                $this->error('获取失败');
            }
        } else {
            $this->error(ErrorCode::$ERROR['ERROR_METHOD']);
        }
    }

    // 测试
    public function test()
    {
//        halt(file_exists(ROOT_PATH.'public/'."/uploads/20201217/5e3009df0e0a5ea432b6f2b6d35fe601.jpg"));
//        $result = Db::name('test')->insert(['admin_id'=>1]);
//        halt($result);
//        halt(Sms::check('17853609200', 7723, 'send'));

        // 队列
        $QueueResult = Queue::later(10, 'app\common\queue\End', ['id' => 10], 'job_queue');
        if ($QueueResult !== 1) {
            $this->error('定时任务发布失败');
        }
    }
}
